package com.itbaizhan.shopping_manager_api.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itbaizhan.shopping_common.pojo.Goods;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.GoodsService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * @program: shopping
 * @ClassName GoodsController
 * @description:
 * @author: jing
 * @create: 2025-06-26 21:26
 **/

@RestController
@RequestMapping("/goods")
public class GoodsController {
    @DubboReference
    private GoodsService goodsService;

    @PostMapping("/add")
    public BaseResult add(@RequestBody Goods goods){
        goodsService.add(goods);
        return BaseResult.ok();
    }

    @PutMapping("/update")
    public BaseResult update(@RequestBody Goods goods){
        goodsService.update(goods);
        return BaseResult.ok();
    }

    /**
     * 上架/下架商品
     * @param id 商品id
     * @param isMarketable 是否上架/下架
     * @return
     */
    @PutMapping("/putAway")
    public BaseResult putAway(Long id,Boolean isMarketable){
        goodsService.putAway(id,isMarketable);
        return BaseResult.ok();
    }

    /**
     * 根据id查询商品详情
     * @param id 商品id
     * @return 商品详情
     */
    @GetMapping("/findById")
    public BaseResult findById(Long id){
        Goods goods = goodsService.findById(id);
        return BaseResult.ok(goods);
    }

    /**
     * 分页查询商品详情
     * @param goods
     * @param page
     * @param size
     * @return 查询结果
     */
    @GetMapping("/search")
    public BaseResult<Page<Goods>> search(Goods goods,int page,int size){
        Page<Goods> search = goodsService.search(goods, page, size);
        return BaseResult.ok(search);
    }
}
